package progetto;
import peersim.core.CommonState;
import peersim.core.Protocol;
import peersim.vector.SingleValueHolder;


public class MobilitySpeed implements Protocol {

	//le inizializzo con il loro inizializzatore
	private static double minSpeed;
	
	private static double maxSpeed;
	
	private static final double REAL_MIN_SPEED = 0.50;
	
	private static final double REAL_MAX_SPEED = 2;
	
	
	
	private double currentSpeed;
	
	
	public MobilitySpeed(String prefix) {
		currentSpeed = -1;
		// TODO Auto-generated constructor stub
		//maxSpeed = getNormalValue(REAL_MAX_SPEED);
		//minSpeed = getNormalValue(REAL_MIN_SPEED);
		maxSpeed = -1;
		minSpeed = -1;
		
		
	}
	
	public void initializerValues() {
		if(maxSpeed == -1 && minSpeed == -1) {
			maxSpeed = getNormalValue(REAL_MAX_SPEED);
			minSpeed = getNormalValue(REAL_MIN_SPEED);
			System.out.println("maxSpeed "+ maxSpeed + "minSpeed" + minSpeed);
		}
	}
	
	//2 : x = 1000 : 1
	private double getNormalValue(double value) {
		double size = SquareSize.getIstance().getSize();
    	if(size == -1) 
    		throw new RuntimeException("Size not initialized!!");
		System.out.println("grandezza mondo "+ size);
		return value / size;
	}
	
	
	
	
	public void setMaxValue(double maxValue){
		this.maxSpeed = maxValue;
	}
	
	public void setMinValue(double minValue) {
		this.minSpeed = minValue;
	}
	
	public double getMinValue()  {
		return minSpeed;
	}
	
	public double getMaxValue()  {
		return maxSpeed;
	}
	
	
	
	//potrei fare che è la velocità ora settata di movimento, finchè non la cambio mi faccio dare quella
	public double getCurrentSpeed() {
		return this.currentSpeed;
	}
	
	public void setCurrentSpeed(double speed) {
		
		this.currentSpeed = speed;
	}
	
	/**
	 * 
	 * @return a number between 'minSpeed' and 'maxSpeed'
	 */
	public double generateRandomSpeed() {
		
		//generate a number between 'minSpeed' and 'maxSpeed'
		return (minSpeed + (maxSpeed - minSpeed)*CommonState.r.nextDouble()); 
		
		
	}
	
	public Object clone()
	{
		MobilitySpeed ms = null;
		try { ms = (MobilitySpeed)super.clone(); }
		catch( CloneNotSupportedException e ) {} // never happens
		return ms;
	}
	
	public void resetSpeed() {
		this.currentSpeed = 0;
	}
	

	

}
